#include "eqc.h"
#include "ui_eqc.h"

#include <QFileDialog>
#include <QMessageBox>
#include <QString>
#include <QLabel>
#include <Qt>
#include <QTableView>

using namespace std;

MyEqC::MyEqC(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MyEqC)
, m_sorted(-1)
, m_count(0)
, m_magConvType(-1)
{
 ui->setupUi(this);

 // creates and setup the magnitude's convertion dialog
 m_magSwitcher = new QDialog;
 //m_polyFunction->setWindowFlags(Qt::WindowStaysOnTopHint);
 m_magSwt.setupUi(m_magSwitcher);

 // creates and setup the function dialog
 m_polyFunction = new QDialog;
 //m_polyFunction->setWindowFlags(Qt::WindowStaysOnTopHint);
 m_pFunction.setupUi(m_polyFunction);

 // creates and setup the function dialog
 m_polyFunction_2 = new QDialog;
 //m_polyFunction->setWindowFlags(Qt::WindowStaysOnTopHint);
 m_pFunction_2.setupUi(m_polyFunction_2);

 // creates and setup the function dialog
 m_polyFunction_3 = new QDialog;
 //m_polyFunction->setWindowFlags(Qt::WindowStaysOnTopHint);
 m_pFunction_3.setupUi(m_polyFunction_3);

 // creates and setup the about dialog
 m_about = new QDialog;

 // creates and setup the csv configuration
 m_csvconfig = new QDialog;
 m_csvc.setupUi(m_csvconfig);


 //m_about->setWindowFlags(Qt::WindowStaysOnTopHint);
 m_abt.setupUi(m_about);
 m_about->show();

 // define the connection slot for the buttons at the attribute maneger dialog
 connect(m_magSwt.okButton,              SIGNAL(clicked()), this, SLOT(okmagConvertionClicked()));
 connect(m_pFunction.pushButtonOk,       SIGNAL(clicked()), this, SLOT(okpolyfunctionClicked()));
 connect(m_pFunction.pushButtonCancel,   SIGNAL(clicked()), this, SLOT(cancelpolyfunctionClicked()));
 connect(m_pFunction_2.pushButtonOk,     SIGNAL(clicked()), this, SLOT(okpolyfunctionClicked_2()));
 connect(m_pFunction_2.pushButtonNext,   SIGNAL(clicked()), this, SLOT(nextpolyfunctionClicked_2()));
 connect(m_pFunction_2.pushButtonPrev,   SIGNAL(clicked()), this, SLOT(prevpolyfunctionClicked_2()));
 connect(m_pFunction_2.pushButtonCancel, SIGNAL(clicked()), this, SLOT(cancelpolyfunctionClicked_2()));
 connect(m_pFunction_3.pushButtonOk,     SIGNAL(clicked()), this, SLOT(okpolyfunctionClicked_3()));
 connect(m_pFunction_3.pushButtonNext,   SIGNAL(clicked()), this, SLOT(nextpolyfunctionClicked_3()));
 connect(m_pFunction_3.pushButtonPrev,   SIGNAL(clicked()), this, SLOT(prevpolyfunctionClicked_3()));
 connect(m_pFunction_3.pushButtonCancel, SIGNAL(clicked()), this, SLOT(cancelpolyfunctionClicked_3()));

 connect(m_csvc.pushButtonOk,     SIGNAL(clicked()), this, SLOT(okCSVClicked()));
 connect(m_csvc.pushButtonCancel, SIGNAL(clicked()), this, SLOT(cancelCSVClicked()));

 this->resetMagConvArg();

}

MyEqC::~MyEqC()
{
	delete ui;
}

void MyEqC::on_actionOpen_triggered()
{
 // clear the contents of the property table

 QFileDialog dialog;
 dialog.setFileMode(QFileDialog::AnyFile);

 string extDat(".dat");
 string extDat2(".csv");
 QString fileName = QFileDialog::getOpenFileName(this, tr("Open File"),
   "",  tr("Text files (*.csv *.dat)"));
 string fname = fileName.toLocal8Bit().constData();
 vector<Data> newDataSheet;

 if( fname.rfind(extDat) != string::npos || fname.rfind(extDat2) != string::npos)
 {
  ui->tableWidget->clearContents();
  ui->tableWidget->setRowCount(0);

  CONTROLLER.reset();
  this->m_filesystem.readFile(fname);
 } 
 this->refresh();

 //cleaning previous file settings
 m_pFunction.lineEdit_a_1->clear();
 m_pFunction.lineEdit_b_1->clear();
 m_pFunction.lineEdit_a_2->clear();
 m_pFunction.lineEdit_b_2->clear();
 m_pFunction.lineEdit_w_1->clear();
 m_pFunction.lineEdit_w_2->clear();
}

void MyEqC::on_actionSave_triggered()
{
 string extDAT( ".dat" );
 string extDAT2( ".csv" );
 QFileDialog::Options options;
 QString selectedFilter;
 QString fileName = QFileDialog::getSaveFileName(this,
  ("Save file"),
  "filename",
  ("CSV File (*.csv);;DAT File (*.dat)"),
  &selectedFilter,
  options);
 std::string fname = fileName.toLocal8Bit().constData();
 if (fname.rfind(extDAT) != string::npos)
 {
	 m_filesystem.writeFile(fname);
 }
 if (fname.rfind(extDAT2) != string::npos)
 {
	 m_filesystem.writeFile2(fname);
 }
 
}

void MyEqC::on_actionQuit_triggered()
{
 QCoreApplication::exit();
}

void MyEqC::on_actionAbout_triggered()
{
 m_about->show();
}

void MyEqC::on_actionMagnitude_Function_triggered()
{
 if ( !CONTROLLER.getDataSheet().empty() )
 {
  m_magSwitcher->show();
 }
}

void
MyEqC::okmagConvertionClicked()
{
 
 if (m_magSwt.radioButton->isChecked())
 {
  m_magConvType = 0;
  m_polyFunction->exec();
 }
 else if (m_magSwt.radioButton_2->isChecked())
 {
  if (m_magConvType != 1)
  {
   this->resetMagConvArg();
   m_count = 0;
   m_pFunction_2.magType->setNum(0);
   m_magConvType = 1;
   m_pFunction_2.pushButtonNext->setEnabled(true);
   m_pFunction_2.pushButtonPrev->setDisabled(true);
  }
  m_polyFunction_2->exec();
 }
 else if (m_magSwt.radioButton_3->isChecked())
 {
  if (m_magConvType!= 2)
  {
   this->resetMagConvArg();
   m_count = 0;
   m_pFunction_3.magType->setNum(0);
   m_magConvType = 2;
   m_pFunction_3.pushButtonNext->setEnabled(true);
   m_pFunction_3.pushButtonPrev->setDisabled(true);
  }
  m_polyFunction_3->exec();
 }
}

void MyEqC::okpolyfunctionClicked()
{
 double a1, b1, c1, a2, b2, c2, w1, w2;

 // eq. I's parameters
 a1 = m_pFunction.lineEdit_a_1->text().toDouble();
 b1 = m_pFunction.lineEdit_b_1->text().toDouble();
 c1 = m_pFunction.lineEdit_c_1->text().toDouble();
 // eq. II's parameters
 a2 = m_pFunction.lineEdit_a_2->text().toDouble();
 b2 = m_pFunction.lineEdit_b_2->text().toDouble();
 c2 = m_pFunction.lineEdit_c_2->text().toDouble();
 // weights
 w1 = m_pFunction.lineEdit_w_1->text().toDouble();
 w2 = m_pFunction.lineEdit_w_2->text().toDouble();

 if (w1 + w2 == 1.0 && w1 >= 0 && w2 >= 0)
 {
  CONTROLLER.setCalcMag(a1,b1,c1,a2,b2,c2,w1,w2);
  this->refresh();
  m_polyFunction->hide();
 }
 else
 {
  string text = "\nThe added weights must sum to 1!\n";
  QString nameMessage(text.c_str());
  QMessageBox msgBox;
  msgBox.setText(nameMessage);
  msgBox.setStandardButtons(QMessageBox::Ok | QMessageBox::Cancel);
  msgBox.setDefaultButton(QMessageBox::Ok);
  msgBox.setIcon(QMessageBox::Warning);
  msgBox.exec();
 }
}

void MyEqC::cancelpolyfunctionClicked()
{
 m_polyFunction->hide();
}

void
MyEqC::nextpolyfunctionClicked_2()
{
 double wa, wmb;

 m_pFunction_2.pushButtonPrev->setEnabled(true);
 wmb = m_pFunction_2.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_2.lineEdit_w_2->text().toDouble();
 if (wmb + wa == 1.0)
 {
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 m_count++;
 m_pFunction_2.magType->setNum(m_count);
 wmb = m_weightsMb[m_count];
 wa  = m_weightsA[m_count]; 
 if (wmb + wa == 1.0)
 {
  QString str;
  m_pFunction_2.lineEdit_w_1->setText(str.setNum(m_weightsMb[m_count]));
  m_pFunction_2.lineEdit_w_2->setText(str.setNum(m_weightsA[m_count]));
 }
 else
 {
  m_pFunction_2.lineEdit_w_1->clear();
  m_pFunction_2.lineEdit_w_2->clear();
 }
 if (m_count == 5)
 {
  m_pFunction_2.pushButtonNext->setDisabled(true);
 }
}

void
MyEqC::prevpolyfunctionClicked_2()
{
 double wa, wmb;

 m_pFunction_2.pushButtonNext->setEnabled(true);
 wmb = m_pFunction_2.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_2.lineEdit_w_2->text().toDouble();
 if (wmb + wa == 1.0)
 {
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 m_count--;
 m_pFunction_2.magType->setNum(m_count);
 wmb = m_weightsMb[m_count];
 wa  = m_weightsA[m_count]; 
 if (wmb + wa == 1.0)
 {
  QString str;
  m_pFunction_2.lineEdit_w_1->setText(str.setNum(m_weightsMb[m_count]));
  m_pFunction_2.lineEdit_w_2->setText(str.setNum(m_weightsA[m_count]));
 }
 else
 {
  m_pFunction_2.lineEdit_w_1->clear();
  m_pFunction_2.lineEdit_w_2->clear();
 }
 if (m_count == 0)
 {
  m_pFunction_2.pushButtonPrev->setDisabled(true);
 }
}

void MyEqC::okpolyfunctionClicked_2()
{
 double wa, wmb;

 double a1, b1, c1, a2, b2, c2;

 // eq. I's parameters
 a1 = m_pFunction_2.lineEdit_a_1->text().toDouble();
 b1 = m_pFunction_2.lineEdit_b_1->text().toDouble();
 c1 = m_pFunction_2.lineEdit_c_1->text().toDouble();
 // eq. II's parameters
 a2 = m_pFunction_2.lineEdit_a_2->text().toDouble();
 b2 = m_pFunction_2.lineEdit_b_2->text().toDouble();
 c2 = m_pFunction_2.lineEdit_c_2->text().toDouble();

 wmb = m_pFunction_2.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_2.lineEdit_w_2->text().toDouble();

 if (wmb + wa == 1.0)
 {
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 CONTROLLER.setCalcMag(a1,b1,c1,a2,b2,c2,m_weightsMb,m_weightsA);
 this->refresh();
 m_polyFunction_2->hide();

}

void MyEqC::cancelpolyfunctionClicked_2()
{
 m_polyFunction_2->hide();
}


void
MyEqC::nextpolyfunctionClicked_3()
{
 double wa, wmb;

 m_pFunction_3.pushButtonPrev->setEnabled(true);
 // weights
 wmb = m_pFunction_3.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_3.lineEdit_w_2->text().toDouble();
 if (wmb + wa == 1.0)
 {
  // eq. I's parameters
  m_a1[m_count] = m_pFunction_3.lineEdit_a_1->text().toDouble();
  m_b1[m_count] = m_pFunction_3.lineEdit_b_1->text().toDouble();
  m_c1[m_count] = m_pFunction_3.lineEdit_c_1->text().toDouble();
  // eq. II's parameters
  m_a2[m_count] = m_pFunction_3.lineEdit_a_2->text().toDouble();
  m_b2[m_count] = m_pFunction_3.lineEdit_b_2->text().toDouble();
  m_c2[m_count] = m_pFunction_3.lineEdit_c_2->text().toDouble();
  // weights
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 m_count++;
 m_pFunction_3.magType->setNum(m_count);
 wmb = m_weightsMb[m_count];
 wa  = m_weightsA[m_count]; 
 if (wmb + wa == 1.0)
 {
  QString str;
  m_pFunction_3.lineEdit_w_1->setText(str.setNum(m_weightsMb[m_count]));
  m_pFunction_3.lineEdit_w_2->setText(str.setNum(m_weightsA[m_count]));

  m_pFunction_3.lineEdit_a_1->setText(str.setNum(m_a1[m_count]));
  m_pFunction_3.lineEdit_b_1->setText(str.setNum(m_b1[m_count]));
  m_pFunction_3.lineEdit_c_1->setText(str.setNum(m_c1[m_count]));
  m_pFunction_3.lineEdit_a_2->setText(str.setNum(m_a2[m_count]));
  m_pFunction_3.lineEdit_b_2->setText(str.setNum(m_b2[m_count]));
  m_pFunction_3.lineEdit_c_2->setText(str.setNum(m_c2[m_count]));
 }
 else
 {
  m_pFunction_3.lineEdit_w_1->clear();
  m_pFunction_3.lineEdit_w_2->clear();

  m_pFunction_3.lineEdit_a_1->clear();
  m_pFunction_3.lineEdit_b_1->clear();
  m_pFunction_3.lineEdit_c_1->clear();
  m_pFunction_3.lineEdit_a_2->clear();
  m_pFunction_3.lineEdit_b_2->clear();
  m_pFunction_3.lineEdit_c_2->clear();
 }
 if (m_count == 5)
 {
  m_pFunction_3.pushButtonNext->setDisabled(true);
 }
}

void
MyEqC::prevpolyfunctionClicked_3()
{
 double wa, wmb;

 m_pFunction_3.pushButtonNext->setEnabled(true);
 wmb = m_pFunction_3.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_3.lineEdit_w_2->text().toDouble();
 if (wmb + wa == 1.0)
 {
  // eq. I's parameters
  m_a1[m_count] = m_pFunction_3.lineEdit_a_1->text().toDouble();
  m_b1[m_count] = m_pFunction_3.lineEdit_b_1->text().toDouble();
  m_c1[m_count] = m_pFunction_3.lineEdit_c_1->text().toDouble();
  // eq. II's parameters
  m_a2[m_count] = m_pFunction_3.lineEdit_a_2->text().toDouble();
  m_b2[m_count] = m_pFunction_3.lineEdit_b_2->text().toDouble();
  m_c2[m_count] = m_pFunction_3.lineEdit_c_2->text().toDouble();
  // weights
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 m_count--;
 m_pFunction_3.magType->setNum(m_count);
 wmb = m_weightsMb[m_count];
 wa  = m_weightsA[m_count]; 
 if (wmb + wa == 1.0)
 {
  QString str;
  m_pFunction_3.lineEdit_w_1->setText(str.setNum(m_weightsMb[m_count]));
  m_pFunction_3.lineEdit_w_2->setText(str.setNum(m_weightsA[m_count]));

  m_pFunction_3.lineEdit_a_1->setText(str.setNum(m_a1[m_count]));
  m_pFunction_3.lineEdit_b_1->setText(str.setNum(m_b1[m_count]));
  m_pFunction_3.lineEdit_c_1->setText(str.setNum(m_c1[m_count]));
  m_pFunction_3.lineEdit_a_2->setText(str.setNum(m_a2[m_count]));
  m_pFunction_3.lineEdit_b_2->setText(str.setNum(m_b2[m_count]));
  m_pFunction_3.lineEdit_c_2->setText(str.setNum(m_c2[m_count]));
 }
 else
 {
  m_pFunction_3.lineEdit_w_1->clear();
  m_pFunction_3.lineEdit_w_2->clear();

  m_pFunction_3.lineEdit_a_1->clear();
  m_pFunction_3.lineEdit_b_1->clear();
  m_pFunction_3.lineEdit_c_1->clear();
  m_pFunction_3.lineEdit_a_2->clear();
  m_pFunction_3.lineEdit_b_2->clear();
  m_pFunction_3.lineEdit_c_2->clear();
 }
 if (m_count == 0)
 {
  m_pFunction_3.pushButtonPrev->setDisabled(true);
 }
}

void MyEqC::okpolyfunctionClicked_3()
{
 double wa, wmb;

 // eq. I's parameters
 m_a1[m_count] = m_pFunction_3.lineEdit_a_1->text().toDouble();
 m_b1[m_count] = m_pFunction_3.lineEdit_b_1->text().toDouble();
 m_c1[m_count] = m_pFunction_3.lineEdit_c_1->text().toDouble();
 // eq. II's parameters
 m_a2[m_count] = m_pFunction_3.lineEdit_a_2->text().toDouble();
 m_b2[m_count] = m_pFunction_3.lineEdit_b_2->text().toDouble();
 m_c2[m_count] = m_pFunction_3.lineEdit_c_2->text().toDouble();

 wmb = m_pFunction_3.lineEdit_w_1->text().toDouble();
 wa  = m_pFunction_3.lineEdit_w_2->text().toDouble();

 if (wmb + wa == 1.0)
 {
  m_weightsMb[m_count] = wmb;
  m_weightsA[m_count]  = wa;
 }
 CONTROLLER.setCalcMag(m_a1,m_b1,m_c1,m_a2,m_b2,m_c2,m_weightsMb,m_weightsA);
 this->refresh();
 m_polyFunction_3->hide();

}

void MyEqC::cancelpolyfunctionClicked_3()
{
 m_polyFunction_3->hide();
}

void MyEqC::refresh()
{
 ui->tableWidget->clearContents();
 ui->tableWidget->setRowCount(0);

 vector<Data> newDataSheet = CONTROLLER.getDataSheet();

 QString txt;

 for(std::vector<Data>::iterator it = newDataSheet.begin(); it != newDataSheet.end(); ++it)
 {

  QTableWidgetItem *sizeItem1  = new QTableWidgetItem; QTableWidgetItem *sizeItem2 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem3  = new QTableWidgetItem; QTableWidgetItem *sizeItem4 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem5  = new QTableWidgetItem; QTableWidgetItem *sizeItem6 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem7  = new QTableWidgetItem; QTableWidgetItem *sizeItem8 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem9  = new QTableWidgetItem; QTableWidgetItem *sizeItem10 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem11 = new QTableWidgetItem; QTableWidgetItem *sizeItem12 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem13 = new QTableWidgetItem; QTableWidgetItem *sizeItem14 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem15 = new QTableWidgetItem; QTableWidgetItem *sizeItem16 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem17 = new QTableWidgetItem; QTableWidgetItem *sizeItem18 = new QTableWidgetItem;
  QTableWidgetItem *sizeItem19 = new QTableWidgetItem;


  int row = ui->tableWidget->rowCount();
  ui->tableWidget->insertRow(row);

  txt.setNum(it->getyear());       sizeItem1->setText( txt );   ui->tableWidget->setItem(row, 0, sizeItem1);
  txt = it->getdate().c_str();     sizeItem2->setText( txt );   ui->tableWidget->setItem(row, 1, sizeItem2);
  txt = it->gethour().c_str();     sizeItem3->setText( txt );   ui->tableWidget->setItem(row, 2, sizeItem3);
  txt.setNum(it->getlat(),'f',2);  sizeItem4->setText( txt );   ui->tableWidget->setItem(row, 3, sizeItem4);
  txt.setNum(it->getlong(),'f',2); sizeItem5->setText( txt );   ui->tableWidget->setItem(row, 4, sizeItem5);
  txt.setNum(it->geterror());      sizeItem6->setText( txt );   ui->tableWidget->setItem(row, 5, sizeItem6);
  txt = it->getstate().c_str();    sizeItem7->setText( txt );   ui->tableWidget->setItem(row, 6, sizeItem7);
  txt.setNum(it->getdepth());      sizeItem8->setText( txt );   ui->tableWidget->setItem(row, 7, sizeItem8);
  txt.setNum(it->getmag(),'f',1);  sizeItem9->setText( txt );   ui->tableWidget->setItem(row, 8, sizeItem9);
  txt.setNum(it->gettype());       sizeItem10->setText( txt );  ui->tableWidget->setItem(row, 9, sizeItem10);
  txt = it->getcat();              sizeItem11->setText( txt );  ui->tableWidget->setItem(row, 10, sizeItem11);
  txt = it->getIo().c_str();       sizeItem12->setText( txt );  ui->tableWidget->setItem(row, 11, sizeItem12);
  txt.setNum(it->getarea());       sizeItem13->setText( txt );  ui->tableWidget->setItem(row, 12, sizeItem13);
  txt.setNum(it->getMw1(),'f',2);  sizeItem16->setText( txt );  ui->tableWidget->setItem(row, 13, sizeItem16);//Mw1
  txt.setNum(it->getMw2(),'f',2);  sizeItem17->setText( txt );  ui->tableWidget->setItem(row, 14, sizeItem17);//Mw2
  txt.setNum(it->getMw3(),'f',2);  sizeItem18->setText( txt );  ui->tableWidget->setItem(row, 15, sizeItem18);//Mw3
  txt.setNum(it->getMw(),'f',2);   sizeItem19->setText( txt );  ui->tableWidget->setItem(row, 16, sizeItem19);//Mw
  txt = it->getlocal().c_str();    sizeItem14->setText( txt );  ui->tableWidget->setItem(row, 17, sizeItem14);
  txt = it->getcomment().c_str();  sizeItem15->setText( txt );  ui->tableWidget->setItem(row, 18, sizeItem15);


  sizeItem1->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);  
  sizeItem2->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem3->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem4->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem5->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem6->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem7->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem8->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem9->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem10->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem11->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem12->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem13->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem14->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem15->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem16->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem17->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem18->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);
  sizeItem19->setFlags(Qt::ItemIsSelectable | Qt::ItemIsUserCheckable | Qt::ItemIsEnabled | Qt::ItemIsDragEnabled);

  sizeItem1->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);  sizeItem2->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem3->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);  sizeItem4->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem5->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);  sizeItem6->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem7->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);  sizeItem8->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem9->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);  sizeItem10->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem11->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); sizeItem12->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem13->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); sizeItem14->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem15->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); sizeItem16->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem17->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter); sizeItem18->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);
  sizeItem19->setTextAlignment(Qt::AlignHCenter | Qt::AlignVCenter);

 }

}

void MyEqC::on_actionSort_triggered()
{

 QList <QTableWidgetItem*> selectedItens = 	ui->tableWidget->selectedItems();
 QList <QTableWidgetItem*>::iterator it;
 it = selectedItens.begin();
 if (selectedItens.size() != 0)
 {
  int col = (*it)->column();
  if (m_sorted!=col)
  {
   /*Qt::SortOrder order = Qt::AscendingOrder;
   ui->tableWidget->sortItems(col, order);*/
   CONTROLLER.sort((PROP) col);
   m_sorted = col;
  }
  else
  {   
   /*Qt::SortOrder order = Qt::DescendingOrder;
   ui->tableWidget->sortItems(col, order);
   m_sorted = -1;*/
   CONTROLLER.invert();
  }
  this->refresh();
  ui->tableWidget->setCurrentCell ( 0, col );
 }
}

void MyEqC::on_actionCSV_Configuration_triggered()
{
	m_csvconfig->show();
}

void MyEqC::okCSVClicked()
{
	if (m_csvc.radioButtonComma->isChecked())
	{
		CONTROLLER.setCharacter(",");
	}
	else if (m_csvc.radioButtonSemicolon->isChecked())
	{
		CONTROLLER.setCharacter(";");
	}

	m_csvconfig->hide();
}

void MyEqC::cancelCSVClicked()
{
	m_csvconfig->hide();
}

void MyEqC::resetMagConvArg()
{
 for (unsigned int i = 0; i < 6; ++i)
 {
  m_a1[i]=0;
  m_b1[i]=0;
  m_c1[i]=0;
  m_a2[i]=0;
  m_b2[i]=0;
  m_c2[i]=0;
  m_weightsMb[i]=0;
  m_weightsA[i]=0;
 }
}



